﻿<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
<title>项目工厂  -- 2010清洁工版</title>
<hta:application
	applicationname="htanotepad" id="ohta" border="thick"
	borderstyle="normal" caption="yes" contextmenu="yes"
	innerborder="no" maximizebutton="yes" minimizebutton="yes"
	navigable="yes"
	icon="notepad.exe" scroll="no" scrollflat="no"
	selection="no" showintaskbar="yes" singleinstance="no"
	sysmenu="yes" version="0.3" windowstate="normal"/>
<script src="Common.js"></script>
<script src="Local.js"></script>
<style type="text/css">
body{margin:0;font-family:"宋体";font-size:12px;}
textarea{width:100%;height:100%;}
table,td{width:100%;height:100%;}
.setting{height:25px;}
.options{height:225px;}
#text_path{width:500px;}
#text_filter{width:230px;}
#userData{behavior:url(#default#userdata);}
</style>
</head>
<body leftmargin="0" topmargin="0" rightmargin="0" scroll="no">
<input id="userdata" type="hidden"/>
<table colspan="0" rowspan="0">
	<tr><td class="setting">
		模板目录：<input id="text_templatePath" class="userData" title="模板目录" value="D:\temp\template"/><input id="button_templatePath" type="button" value="选择目录"/>&nbsp;
		项目目录：<input id="text_projectPath" title="项目目录" value="D:\temp\project"/><input id="button_projectPath" type="button" value="选择目录"/>&nbsp;
	</td></tr>
	<tr><td class="setting">
		<input id="button_process" type="button" value="生成"/>&nbsp;<input id="button_close" type="button" value="退出"/>
	</td></tr>
	<tr><td class="options">
		<textarea id="textarea_options" wrap="off">{
	values: {
		"project": "demo" // 项目名
	}
}</textarea>
	</td><tr>
	<tr><td>
		<textarea id="textarea_log" wrap="off"></textarea>
	</td><tr>
</table>
<script type="text/javascript">
var options = {}; // 配置
 
function replaceText(text) {
	if (!text) return text;
	var names = [], dict = options.values;
	for (var p in dict) {
		names.push(p);
	}
	if (!names.length) return text;
	var regExp = new RegExp("\\b(" + names.join("|") + ")\\b", "gi");
	return text.replace(regExp, function(all, key) {
		return dict[key];
	});
}

function replaceFile(filename) {
	var text = Local.readFileText(filename),
		dict = options.values, 
		newText = replaceText(text);
	if (text != newText) {
		Local.writeFileText(filename, newText);
		return true;
	}
}

function executeCommand(command) {
	if (!command) return;
	switch (command.type) {
		case "shortcut":
			Local.createShortcut(
				replaceText(command.filename),
				replaceText(command.target),
				replaceText(command.arguments),
				replaceText(command.wroking)
			);
			return Common.format('创建快捷方式 "${0}" ', [replaceText(command.filename)]); 
	}
}

Common.setEvent(window, "load", function() {
	var userdata = Common.$("userdata");
	userdata.load("project-builder");
	Common.$("text_templatePath").value = userdata.getAttribute("templatePath") || Common.$("text_templatePath").value; 
	Common.$("text_projectPath").value = userdata.getAttribute("projectPath") || Common.$("text_projectPath").value; 
	Common.$("textarea_options").value = userdata.getAttribute("options") || Common.$("textarea_options").value;

	Common.$("button_close").onclick = function() {
		window.close();
	};

	Common.$("button_templatePath").onclick = function() {
		var folder = Local.folderDialog(Common.$("text_templatePath").title);
		if (!folder) return;
		Common.$("text_templatePath").value = folder;
	};

	Common.$("button_projectPath").onclick = function() {
		var folder = Local.folderDialog(Common.$("text_projectPath").title);
		if (!folder) return;
		Common.$("text_projectPath").value = folder;
	};

	Common.$("button_process").onclick = function() {
		var filenames = [];
		var foldernames = [];
		options = eval("(" + Common.$("textarea_options").value + ")");
		options.values = options.values || {};
		options.values.DESKTOP_PATH = Local.specialFolders("Desktop");
		options.values.MYDOCUMENTS_PATH = Local.specialFolders("MyDocuments");

		options.values.TEMPLATE_PATH = options.values.templatePath = replaceText(Common.$("text_templatePath").value);
		options.values.PROJECT_PATH = options.values.projectPath = replaceText(Common.$("text_projectPath").value);
		
		userdata.setAttribute("templatePath", Common.$("text_templatePath").value); 
		userdata.setAttribute("projectPath", Common.$("text_projectPath").value); 
		userdata.setAttribute("options", Common.$("textarea_options").value);
		userdata.save("project-builder");

		if (!options.values.projectPath) {
			alert("未设置项目路径");
			return;
		}
		if (Local.folderExists(options.values.projectPath) &&
			!confirm(Common.format('目录  "${projectPath}" 已经存在确认删除？', options.values)) 
		) {
			return;
		}
		// 删除目录
		var command = Common.format('cmd.exe /c del "${projectPath}\\*.*" /s/q', options.values);
		Local.commandLine(command);
		Common.$("textarea_log").value += command + "\n";

		var command = Common.format('cmd.exe /c rd "${projectPath}" /s/q', options.values);
		Local.commandLine(command);
		Common.$("textarea_log").value += command + "\n";

		// 复制目录
		var command = Common.format('xcopy "${templatePath}" "${projectPath}" /e/r/y/i', options.values);
		Local.commandLine(command);
		Common.$("textarea_log").value += command + "\n";
		Local.scanFolder(options.values.projectPath, 
			function(filename) {
				filenames.push(filename);
			},
			function(foldername) {
				foldernames.unshift(foldername);
			}
		);
		for (var i = 0; i < filenames.length; i++) {
			var filename = filenames[i];
			
			var newfilename = replaceText(filename);
			if (newfilename != filename) {
				Local.moveFile(filename, newfilename);
				Common.$("textarea_log").value += Common.format('"${0}" -> "${1}" 重命名。\n', [filename, newfilename]);
			}

			if (replaceFile(newfilename, options.values))
				Common.$("textarea_log").value += Common.format('"${0}" 内容被替换 。\n', [newfilename]);
		}

		for (var i = 0; i < foldernames.length; i++) {
			var foldername = foldernames[i];
			var newfoldername = replaceText(foldername);
			if (newfoldername != foldername) {
				Local.moveFolder(foldername, newfoldername);
				Common.$("textarea_log").value += Common.format('"${0}"" -> "${1}" 重命名。\n', [foldername, newfoldername]);
			}
		}
		for (var p in options.commands) {
			var r = executeCommand(options.commands[p]);
			if (r) Common.$("textarea_log").value += Common.format('命令：${0}。\n', [r]);
		}
		
		// 打开项目目录
		var command = Common.format('cmd.exe /c start explorer.exe "${projectPath}"', options.values);
		Local.commandLine(command);
		Common.$("textarea_log").value += command + "\n";
	}
});
</script>
</body>
</html>